//
//  LocaleFileManager.m
//  DTLibrary
//
//  Created by Leks Zhang on 11-3-18.
//  Copyright 2011 Leks Zhang. All rights reserved.
//

#import "FileManager.h"

@implementation FileManager

static FileManager *singleton = nil;
+ (id)sharedInstance {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        singleton = [[[self class] alloc] init];
    });
    return singleton;
}

//几大文件目录

+ (NSString *)getHomeDirectory {
    NSString *homePath = NSHomeDirectory();
//    NSLog(@"Home目录：%@",homePath);
    return homePath;
}

+ (NSString *)getDocumentsDirectory {
    NSArray *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
    NSString *documentsPath = docPath[0];
//    NSLog(@"Documents目录：%@",documentsPath);
    return documentsPath;
}

+ (NSString *)getLibraryDirectory {
    NSArray *libsPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libPath = libsPath[0];
//    NSLog(@"Library目录：%@",libPath);
    return libPath;
}

+ (NSString *)getCacheDirectory {
    NSArray *cacPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachePath = cacPath[0];
//    NSLog(@"Cache目录：%@",cachePath);
    return cachePath;
}

+ (NSString *)getTemporaryDirectory {
    NSString *tempPath = NSTemporaryDirectory();
//    NSLog(@"temp目录：%@",tempPath);
    return tempPath;
}

#pragma -mark File Operation
- (BOOL)creatDirectory:(NSString *)dirPath {
    if ([Common isEmptyString:dirPath]) {
        return NO;
    }
    NSFileManager *fm = [NSFileManager defaultManager];
    NSError *error = nil;
    BOOL resBool = [fm createDirectoryAtPath:dirPath
                 withIntermediateDirectories:YES
                                  attributes:nil
                                       error:&error];
    if (error) {
        NSLog(@"error:%@",[error description]);
        return NO;
    }
    return resBool;
}

- (BOOL)creatFile:(NSString *)filePath Data:(id)data Name:(NSString *)name {
    if ([Common isEmptyString:filePath] ||
        !data ||
        [Common isEmptyString:name]) {
        return NO;
    }
    NSFileManager *fm = [NSFileManager defaultManager];
    //不存在则创建目录
    if (![fm fileExistsAtPath:filePath]) {
        [[FileManager sharedInstance] creatDirectory:filePath];
    }
    NSError *error = nil;
    BOOL resBool = [fm createFileAtPath:[NSString stringWithFormat:@"%@/%@",filePath,name]
                               contents:data
                             attributes:nil];
    if (error) {
        NSLog(@"error:%@",[error description]);
        return NO;
    }
    return resBool;
}

- (BOOL)removeFileOrDirectory:(NSString *)path {
    if ([Common isEmptyString:path]) {
        return NO;
    }
    NSFileManager *fm = [NSFileManager defaultManager];
    NSError *error = nil;
    BOOL resBool = [fm removeItemAtPath:path
                                  error:&error];
    if (error) {
        NSLog(@"error:%@",[error description]);
        return NO;
    }
    return resBool;
}

//递归查找
- (NSArray *)findFileContentsPath:(NSString *)path {
    if ([Common isEmptyString:path]) {
        return nil;
    }
    NSFileManager *fm = [NSFileManager defaultManager];
    NSError *error = nil;
    NSArray *resArr = [fm subpathsOfDirectoryAtPath:path
                                              error:&error];
    if (error) {
        NSLog(@"error:%@",[error description]);
        return nil;
    }
    return resArr;
}

//单个文件的大小
- (long long)fileSizeAtPath:(NSString*) filePath {
    NSFileManager *fm = [NSFileManager defaultManager];
    if ([fm fileExistsAtPath:filePath]) {
        NSError *error = nil;
        long long sumSize = [[fm attributesOfItemAtPath:filePath
                                                  error:&error] fileSize];
        if (error) {
            NSLog(@"error:%@",[error description]);
            return 0;
        }
        return sumSize;
    }
    return 0;
}

//遍历文件夹获得文件夹大小，返回多少M
- (float)folderSizeAtPath:(NSString*)folderPath {
    NSFileManager *fm = [NSFileManager defaultManager];
    if (![fm fileExistsAtPath:folderPath]) {
        return 0;
    }
    NSEnumerator *childFilesEnumerator = [[fm subpathsAtPath:folderPath] objectEnumerator];
    NSString *fileName = nil;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        NSString *fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    return folderSize/(1024.0*1024.0);
}

@end
